/**
 * Copyright (c) 2008 michiyasu wada
 * http://www.seyself.com/
 * 
 * Distributed under The MIT License.
 * [http://www.opensource.org/licenses/mit-license.php]
 */

package demo.objects 
{
import flash.display.Shape;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.display.MovieClip;
import jp.progression.commands.Func;
import jp.progression.commands.AddChild;
import jp.progression.commands.RemoveAllChild;
import jp.progression.Progression;
import jp.progression.casts.CastSprite;
import jp.progression.casts.CastButton;
import jp.progression.commands.DoTweener;
import jp.progression.scenes.SceneId;
import jp.progression.scenes.SceneObject;
import demo.scenes.Main;
import demo.modules.ScrollBar;
import demo.modules.ScrollType;
import demo.scenes.CategoryScene;
import demo.buttons.ItemClipButton;
import demo.objects.items.MenuCast;


public class BodyCast extends CastSprite
{
    //--------------------------------------------------------------------------
    //  Properties
    //--------------------------------------------------------------------------
    public function get menu():MenuCast
    {
        return MenuCast(getChildByName("Menu"));
    }
    
    private var selectedItem:Object;
    private var wheelCast:CastButton;
    
    private var _scrollbar:ScrollBar;
    public function get scrollbar():ScrollBar { return _scrollbar; }
    public function get items():CastSprite { return _items; }
    
    //--------------------------------------------------------------------------
    //  Constructor
    //--------------------------------------------------------------------------
    public function BodyCast() 
    {
        var bar = getChildByName("dragbarObject");
        var bg = getChildByName("backgroundObject");
        _scrollbar = new ScrollBar( bar, bg, ScrollType.VERTICAL );
        _scrollbar.target = items;
        _scrollbar.scrollMask = getChildByName("BodyMask");
        _scrollbar.margin.top = 0;
        _scrollbar.targetMargin.top = 0;
        _scrollbar.targetMargin.bottom = 0;
        _scrollbar.speed = 0.3;
        
        wheelCast = new CastButton();
        wheelCast.graphics.beginFill( 0x000000, 0.0 );
        wheelCast.graphics.drawRect( 0, 0, 800, 200 );
        wheelCast.graphics.endFill();
        wheelCast.useHandCursor = false;
        wheelCast.x = -10;
        wheelCast.y = 200;
        addChildAt( wheelCast, 0 );
        
        
        addEventListener( MouseEvent.MOUSE_WHEEL, mouseWheelHandler );
    }
    
    //--------------------------------------------------------------------------
    //  Public Methods
    //--------------------------------------------------------------------------
    public function selection( item:Object ):void
    {
        if ( selectedItem ) {
            new DoTweener( selectedItem.background , { alpha: 0.0 , time: 0.3 } ).execute(); 
            selectedItem.buttonEnabled = true;
        }
        
        selectedItem = item;
        
        if ( selectedItem ) {
            selectedItem.buttonEnabled = false;
            new DoTweener( selectedItem.background , { alpha: 0.3 , time: 0.3 } ).execute();
        }
    }
    
    public function setData( data:XML ):void
    {
        var nodes:XMLList = data.children();
        
        items.removeAllChildren();
        scrollbar.reset();
        
        var n:int = nodes.length();
        for (var i:int = 0; i < n; i++ ) {
            if(nodes[i]){
                var clip:ItemClipButton = new ItemClipButton(nodes[i]);
                new AddChild( items, clip , true ).execute();
                clip.y = i * 120;
            }
        }
    }
    
    public function removeItems():void
    {
        new RemoveAllChild( items ).execute();
    }
    
    //--------------------------------------------------------------------------
    //  Override Methods
    //--------------------------------------------------------------------------
    override protected function _onCastAdded():void
    {
        stage.addEventListener( Event.RESIZE, resizeHandler );
        resizeHandler();
        
        alpha = 0.0;
        var ny = 109;
        y += 100;
        
        addCommand(
            new DoTweener( this, { alpha: 1.0, y:ny, time: 0.8 }, 600 ),
            new Func( this , setup )
        );
        
    }
    
    override protected function _onCastRemoved():void
    {
        addCommand(
            new DoTweener( this, { alpha: 0.0, y: 209, time: 0.3 } )
        );
    }
    
    //--------------------------------------------------------------------------
    //  Event Handler Methods
    //--------------------------------------------------------------------------
    private function resizeHandler( event:Event=null ):void
    {
        var h:Number = stage.getChildAt(0).height - 400;
        scrollbar.background.height = h;
        scrollbar.reset();
        getChildByName("BodyMask").height = h;
        wheelCast.height = h+30;
    }
    
    private function mouseWheelHandler( event:MouseEvent ):void
    {
        var n:Number = event.delta < 0 ? 0.1 : -0.1;
        scrollbar.scroll( scrollbar.scrollPosition + n );
    }
    
    //--------------------------------------------------------------------------
    //  Private Methods
    //--------------------------------------------------------------------------
    private function setup()
    {
        var scene = Main.instance.getChildByName( "search" );
        var len:int = scene.numChildren;
        for (var i = 0; i < len-1;i++ ) {
            buttonSetting( CastButton(getChildByName("tab_"+i)), CategoryScene(scene.getChildAt(i)) );
        }
        
    }
    
    private function buttonSetting( target:CastButton , scene:CategoryScene )
    {
        var loc = this;
        
        scene.sceneTab = target;
        target.sceneId = scene.sceneId;
        target.toolTip.text = scene.name;
        target.onCastClick = function()
        {
            loc.selection( this );
        }
        target.onCastMouseOver = function()
        {
            new DoTweener( target.background , { alpha: 0.3 , time: 0.3 } ).execute(); 
        }
        target.onCastMouseOut = function()
        {
            new DoTweener( target.background , { alpha: 0.0 , time: 0.3 } ).execute(); 
        }
    }
    
    
}

}